package com.example.sprintboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.sprintboot.entity.User;
import com.example.sprintboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/users")
@PreAuthorize("hasRole('ADMIN')")
public class AdminUserController {

    @Autowired
    private UserService userService;

    // 用户列表
    @GetMapping
    public Object list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String role,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String keyword
    ) {
        IPage<User> userPage = userService.adminListUsers(page, pageSize, role, status, keyword);
        return new java.util.HashMap<String, Object>() {{
            put("list", userPage.getRecords());
            put("total", userPage.getTotal());
        }};
    }

    // 启用/禁用用户
    @PostMapping("/{id}/toggle")
    public void toggleUser(@PathVariable Integer id) {
        userService.toggleUserStatus(id);
    }

    // 重置密码
    @PostMapping("/{id}/reset-password")
    public void resetPassword(@PathVariable Integer id) {
        userService.resetUserPassword(id);
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Integer id) {
        userService.deleteUserByAdmin(id);
    }
} 